package com.jt.vo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.annotation.RequiredLog;
import com.jt.mapper.LogMapper;
import com.jt.pojo.Log;
import com.jt.service.LogService;
import jdk.nashorn.internal.objects.NativeDebug;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
public class LogAspect extends WebUtils{
    @Autowired
    private LogMapper logMapper;

    @Pointcut("@annotation(com.jt.annotation.RequiredLog)")
    public void doLog(){}

    @Around("doLog()")
    public Object doAround(ProceedingJoinPoint joinPoint)throws Throwable{
        int status=1;
        String error=null;
        long time=0l;
        long t1=System.currentTimeMillis();
        try {

            Object result = joinPoint.proceed();
            long t2=System.currentTimeMillis();
            time=t2-t1;
            return result;
        }catch (Throwable e){
            long t3=System.currentTimeMillis();
            time=t3-t1;
            status=0;
            error=e.getMessage();
            throw e;
        }finally {
            saveLog(joinPoint,time,status,error);
        }

}

    private void saveLog(ProceedingJoinPoint joinPoint, long time, int status, String error) throws NoSuchMethodException, JsonProcessingException {
        Class<?> targetClass = joinPoint.getTarget().getClass();

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method targetMethod =
                targetClass.getDeclaredMethod(signature.getName(), signature.getParameterTypes());
        RequiredLog requiredLog = targetMethod.getAnnotation(RequiredLog.class);
        String operation = requiredLog.value();
        String targetMethodName = targetClass.getName() + "." + targetMethod.getName();
        String params = new ObjectMapper().writeValueAsString(joinPoint.getArgs());
        String username = "121";
//        String username = (String)
//                SecurityContextHolder.getContext()
//                        .getAuthentication()
//                        .getPrincipal();

        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String ip = requestAttributes.getRequest().getRemoteAddr();

        Log logInfo = new Log();
        logInfo.setIp(ip);//后续获取
        logInfo.setUsername(username);
        logInfo.setParams(params);
        logInfo.setTime(time);
        logInfo.setError(error);

        logInfo.setCreatedtime(new Date());
//        System.out.println("logInfo=" + logInfo);
        logService.insertLog(logInfo);


        }
        @Autowired
    private  LogService logService;
    }

